import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:gluz/app/common/aliyun_oss.dart';
import 'package:gluz/app/common/extension/string_extension.dart';
import 'package:gluz/app/data/api_response.dart';
import 'package:gluz/app/data/country_entity.dart';
import 'package:gluz/app/data/user_profile_entity.dart';
import 'package:gluz/app/utils/http_util.dart';
import 'package:gluz/app/utils/toast_util.dart';

import '../../../../app_session.dart';
import '../../../common/bottom_modal_sheet.dart';
import '../../../common/resource_picker_sheet.dart';
import '../../../routes/app_pages.dart';
import '../../../utils/h5_url_manager.dart';

class LoginController extends GetxController {
  TextEditingController phoneController = TextEditingController();
  TextEditingController pwdController = TextEditingController();
  CountryEntity _selectedCountry = CountryEntity.countryList.first;
  CountryEntity get selectedCountry => _selectedCountry;
  set selectedCountry(CountryEntity value) {
    _selectedCountry = value;
  }
  var isCheckLink = true;

  // 中文语言选择事件
  void onChineseLanguageSelected() {
    // 实现中文语言选择逻辑
    app.translationToCN();
    update();
  }

  // 俄文语言选择事件
  void onRussianLanguageSelected() {
    // 实现俄文语言选择逻辑
    app.translationToRU();
    update();
  }

  /// 选择国家
  void onTapCountry() {
    BottomModalSheet.showCountryList((entity) {
      selectedCountry = entity;
      update();
    });
  }

  // 登录按钮点击事件
  void onLoginButtonPressed() async {

    if (phoneController.text.isPhoneNumerValid(selectedCountry.id) == false) {
      ToastUtil.show('请输入正确的手机号'.tr);
      return;
    }
    if (pwdController.text.trim().length < 8) {
      ToastUtil.show('请输入 8 位以上的密码'.tr);
      return;
    }

    // 实现登录逻辑
    ToastUtil.showLoading();
    TokenEntity? tokenEntity = await app.http.request(
        path: '/api/user/login',
        method: Method.post,
        params: {
      'countryCode': selectedCountry.code,
      'phoneNumber': phoneController.text,
      'password': pwdController.text
    }, fromJson: (data) => TokenEntity.fromJson(data));
    if (tokenEntity == null) return;
    app.setToken(tokenEntity.accessToken);

    UserProfileEntity? userProfileEntity = await app.getUserFromServer();
    if (userProfileEntity == null) return;
    // 登录成功跳转到TAB页面
    ToastUtil.show('登录成功'.tr);

    if (selectedCountry.id == 0) {
      app.translationToCN();
    } else {
      app.translationToRU();
    }
    Get.offNamed(Routes.MAIN_TAB);
  }

  // 忘记密码链接点击事件
  void onForgotPasswordPressed() {
    // 实现忘记密码逻辑
    Get.toNamed(Routes.FORGOT);
  }

  // 验证码登录链接点击事件
  void onVerificationCodeLoginPressed() async {
    // 实现验证码登录逻辑

  }

  // 用户协议点击事件
  void onUserAgreementTapped() {
    // 实现用户协议查看逻辑
    Get.toNamed(
      Routes.WEBVIEW,
      arguments: {
        'url': H5UrlManager.userPrivacyUrl('隐私政策'),
      },
    );
  }

  // 注册链接点击事件
  void onRegisterLinkTapped() {
    // 实现注册跳转逻辑
    Get.toNamed(Routes.REGISTER);
  }

  @override
  void onInit() {
    super.onInit();
    app.translationToCN();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    app.keyboardDismiss();
  }
}
